package com.xoa.controller.inv;

import com.xoa.model.inv.OmShipment;
import com.xoa.service.inv.OmShipmentService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售出货
 */
@Controller
@RequestMapping("/salesShipment")
public class OmShipmentController {
    @Autowired
    private OmShipmentService salesShipmentService;

    //映射主页面
    @RequestMapping("/index")
    public String orderIndex(HttpServletRequest request) {
        return "/app/storage/salesManagement/salesShipment/salesShipment";
    }

    //映射仓库页面
    @RequestMapping("/wareHouse")
    public String wareHouse(HttpServletRequest request) {
        return "/app/storage/salesManagement/addWarehouse";
    }

    //映射添加客户页面
    @RequestMapping("/addCustom")
    public String addCustom(HttpServletRequest request) {
        return "/app/storage/salesManagement/addCustomer";
    }

    //客户欠款还款统计页面
    @RequestMapping("/repayment")
    public String repayment(HttpServletRequest request) {
        return "/app/storage/salesManagement/salesShipment/index";
    }

    //销售出货客户还款页面
    @RequestMapping("/hkym")
    public String hkym(HttpServletRequest request) {
        return "/app/storage/salesManagement/salesShipment/customerRepayment";
    }


    //处理时间  将前台传过来的时间存到数据库中
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    /**
     * 添加数据
     */
    @RequestMapping("/insertSales")
    @ResponseBody
    public ToJson<Integer> insertSales(HttpServletRequest request, OmShipment omShipment, String productType, String enterName, String enterSpecs, String enterCode) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            if (productType != null && productType != "") {
                map.put("productType", productType);
            }
            if (enterName != null && enterName != "") {
                map.put("enterName", enterName);
            }
            if (enterSpecs != null && enterSpecs != "") {
                map.put("enterSpecs", enterSpecs);
            }
            if (enterCode != null && enterCode != "") {
                map.put("enterCode", enterCode);
            }


            Integer data = salesShipmentService.insertSales(omShipment, map);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询所有
     *
     * @param request
     * @param page
     * @param useFlag
     * @return
     */
    @RequestMapping("/selectAllSales")
    @ResponseBody
    public ToJson<OmShipment> selectAll(HttpServletRequest request, Integer page, OmShipment omShipment, Integer limit, boolean useFlag, String productType, String enterName, String enterSpecs, String enterCode, Integer type, Integer isArrears) {
        ToJson json = new ToJson<OmShipment>(1, "err");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            map.put("pageParams", pageParams);
        }catch (Exception e){
            e.getMessage();
        }

        map.put("type", type);
        map.put("isArrears", isArrears);
        map.put("obj", omShipment);
        if (productType != null && productType != "") {
            map.put("productType", productType);
        }
        if (enterName != null && enterName != "") {
            map.put("enterName", enterName);
        }
        if (enterSpecs != null && enterSpecs != "") {
            map.put("enterSpecs", enterSpecs);
        }
        if (enterCode != null && enterCode != "") {
            map.put("enterCode", enterCode);
        }
        try {
            List<OmShipment> omShipments = salesShipmentService.selectAll(map);
            if (omShipments != null) {
                PageParams pageParams =(PageParams) map.get("pageParams");
                json.setTotleNum(pageParams.getTotal());
                json.setObj(omShipments);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 数据回显接口
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public ToJson<OmShipment> selectByPrimaryKey(HttpServletRequest request, Integer shipmentId) {
        ToJson json = new ToJson<OmShipment>(1, "err");
        try {
            OmShipment omShipment = salesShipmentService.selectByPrimaryKey(shipmentId);
            if (omShipment != null) {
                json.setObject(omShipment);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询所有出货人
     */
    @RequestMapping("/selectUsers")
    @ResponseBody
    public ToJson<OmShipment> selectUsers(HttpServletRequest request, Integer shipmentId) {
        return salesShipmentService.selectUsers();
    }

    /**
     * 根据主键id进行删除
     */
    @RequestMapping("/delOmShipmentById")
    @ResponseBody
    public ToJson<Integer> deleteByPrimaryKey(HttpServletRequest request, Integer shipmentId) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Integer data = salesShipmentService.deleteByPrimaryKey(shipmentId);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 更新数据(根据产品id)
     */
    @RequestMapping("/updateOmShipmentById")
    @ResponseBody
    public ToJson<Integer> updateByPrimaryKeySelective(OmShipment omShipment, String productType, String enterName, String enterSpecs, String enterCode) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("productType", productType);
            map.put("enterName", enterName);
            map.put("enterSpecs", enterSpecs);
            map.put("enterCode", enterCode);
            Integer data = salesShipmentService.updateByPrimaryKeySelective(omShipment, productType, enterName, enterSpecs, enterCode);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 产品信息表的导出
     */
    @RequestMapping(value = "/ExportOmShipment", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmShipment> ExportOmShipment(HttpServletRequest request, HttpServletResponse response, OmShipment omShipment, String beginDate, String isExport) {
        return salesShipmentService.ExportOmShipment(request, response, omShipment, beginDate, isExport);
    }

    /**
     * 月销售统计
     */
    @RequestMapping(value = "/shipCount", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmShipment> shipCount(HttpServletRequest request,String year,String warehouseId) {
        return salesShipmentService.shipCount(request, year, warehouseId);
    }

    /**
     * 日销售统计
     */
    @RequestMapping(value = "/shipDayCount", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmShipment> shipDayCount(HttpServletRequest request,String year,String warehouseId,String month) {
        return salesShipmentService.shipDayCount(request, year, warehouseId,month);
    }
    /**
     * 员工销售统计
     */
    @RequestMapping(value = "/shipEmployeeCount", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmShipment> shipEmployeeCount(HttpServletRequest request,String year,String warehouseId,String month,String flag) {
        return salesShipmentService.shipEmployeeCount(request, year, warehouseId,month,flag);
    }
    /**
     * 客户销售统计
     */
    @RequestMapping(value = "/shipCustomerCount", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmShipment> shipCustomerCount(HttpServletRequest request,String year,String warehouseId,String month,String flag) {
        return salesShipmentService.shipCustomerCount(request, year, warehouseId,month,flag);
    }

    /**
     * 其他收支
     */
    @RequestMapping(value = "/shipOtherCount", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmShipment> shipOtherCount(HttpServletRequest request,String year,String warehouseId,String month,String flag,String day) {
        return salesShipmentService.shipOtherCount(request, year, warehouseId,month,flag,day);
    }

}
